/**
 * Created by Administrator on 2017/8/14.
 */

$(document).ready(function () {

    if (C.token==undefined||C.token==null){
        alert('请登录');
        //location.href='login.html';
        againLogin();
        return ;
    }

    var orderProductsId=locationSearcher('orderId');

    var wuliuTpl=$('#select-wuliu-template').html();
    var wuliuCmp=Handlebars.compile(wuliuTpl);

    var productTpl=$('#products-info-template').html();
    var productCmp=Handlebars.compile(productTpl);

    //console.log(orderProductsId);
    selectWuliu();
    function selectWuliu() {
        $.ajax({
            url:C.interface.wuliu,
            type:'get',
            dataType:'json',
            data:{
                token:C.token,
                orderProductsId:orderProductsId
            },
            success:function (response) {
                if (response.result === 'success'){
                    var data=response.data;
                    $('#wuliu-info-list').html(productCmp(data));
                    $('#select-wuliu-info').html(wuliuCmp(data));

                    $('#confirm-goods-button').unbind().bind('click',confirmGoods);
                    $('#confirm-cancel').unbind().bind('click',confirmCancel);
                    $('#confirm-submit').unbind().bind('click',submitConfirmGoods);

                }else if (response.result === 'login'){
                    alert('请重新登录');
                    //location.href='login.html';
                    againLogin();
                }else {
                    alert(response.errorMsg);
                }

            }
        })
    }

    //退款弹窗
    $('#wuliu-tuikuan').unbind().bind('click',tuikuanAlert);

    //拨打客服电话弹窗
    $('#wuliu-kefu').unbind().bind('click',kefuTel);



    //确认收货弹窗的显示
    function confirmGoods() {
        event.preventDefault();

        var thatImg=$('.dis-shop-img').find('img').attr('src');
        var hongbao=$('.dis-shop-detail').find('span').eq(0).html();
        var thatName=$('.dis-shop-detail').find('span').eq(2).html();

        $('.confirm-order-info').find('img').attr('src',thatImg);
        $('.wuliu-alert-box').find('span').eq(0).html(hongbao);
        $('.wuliu-alert-box').find('span').eq(1).html(thatName);

        $('.mask').show();
        $('#confirm-goods').show();
    }

    //取消收货
    function confirmCancel() {
        event.stopPropagation();
        event.preventDefault();
        $('#confirm-goods').hide();
        $('.mask').hide();
    }

    //确认收货
    function submitConfirmGoods() {
        event.preventDefault();
        var loading = dialog.loading();
        $.ajax({
            url:C.interface.confirmGoods,
            type:'get',
            dataType:'json',
            data:{
                token:C.token,
                orderProductsId:orderProductsId
            },
            complete:function () {
                loading.close();
            },
            success:function (response) {
                if (response.result=='success'){
                    location.href='order_pingjia.html';
                }else if (response.result === 'login'){
                    alert('登录过期请重新登录');
                    againLogin();
                }else {
                    alert(response.errorMsg);
                }
            }
        })
    }


    //点击蒙版隐藏所有弹窗
    function maskHide() {

        $('.mask').hide();
        $('.tuikuan-alert').hide();
        $('#confirm-goods').hide();
        $('#kefu-tel-box').hide();
    }
    //联系客服
    function kefuTel() {
        var loading=dialog.loading();
        $.ajax({
            url:C.interface.kefuTel,
            type:'get',
            dataType:'json',
            data:{
                
            },
            complete:function () {
                loading.close();
            },
            success:function (response) {
                if (response.result=='success'){
                    var tel=response.data;
                    $('.tel-box').html(tel);
                    $('.tel-box').attr('href','tel:'+tel);
                    $('.mask').show();
                    $('#kefu-tel-box').show();

                    //取消拨打电话
                    $('.wuliu-close-img').unbind().bind('click',maskHide);

                }
            }
        });


    }

    //退款弹框
    function tuikuanAlert() {

        event.preventDefault();
        /*$('.mask').show();
        $('.tuikuan-alert').show();*/

        dialog.confirm({
            title: "确认要退款吗",
            content: "",
            ok: function () {
                getTuikuanRequest();

            },
            cancel: function () {

            }
        });
    }


    //确认退款
    function getTuikuanRequest() {
        var loading=dialog.loading();
        $.ajax({
            url:C.interface.tuikuan,
            type:'get',
            dataType:'json',
            data:{
                token:C.token,
                orderProductsId:orderProductsId
            },
            complete:function () {
                loading.close();
            },
            success:function (response) {
                if (response.result=='success'){
                    alert('退款请求已发送，请联系客服进行退款操作。');
                }
            }
        })
    }




});
